﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palluk.PassMaster.Passwords
{
    /// <summary>
    /// Is an Ascii table adapter to getting chars array distinguish by kind.
    /// </summary>
    public struct Charset
    {
        private static char[] _lowers;
        private static char[] _numbers;
        private static char[] _symbols;

        /// <summary>
        /// Get an array of lower cases letters. 
        /// </summary>
        /// <returns>A lower letters array.</returns>
        public static char[] GetLowers()
        {
            if (_lowers == null)
                _lowers = Find(c => Char.IsLower(c));
            return _lowers;
        }

        /// <summary>
        /// Get an array of numbers. 
        /// </summary>
        /// <returns>A numbers array.</returns>
        public static char[] GetNumbers()
        {
            if (_numbers == null)
                _numbers = Find(c => Char.IsNumber(c));
            return _numbers;
        }

        /// <summary>
        /// Get an array of symbols. 
        /// </summary>
        /// <returns>A symbols array.</returns>
        public static char[] GetSymbols()
        {
            if (_symbols == null)
                _symbols = Find(c => Char.IsSymbol(c));
            return _symbols;
        }

        /// <summary>
        /// Find all chars in Ascii table that match the condiction defined by the specified predicate.
        /// </summary>
        /// <param name="pattern">The Predicate<char> delegate that define the condiction of search.</param>
        /// <returns>An array of char that matches the condiction.</returns>
        private static char[] Find(Predicate<char> pattern)
        {
            List<char> retArray = new List<char>();
            for (int c = 33; c < 127; c++)
                if (pattern(Convert.ToChar(c)))
                    retArray.Add(Convert.ToChar(c));
            return retArray.ToArray();
        }
    }
}
